function y = logprior(x)
% LOGPRIOR(PARA)
%  computes a log prior density for the structural parameters
%  of the DSGE models
%
% INPUT
%	PARA : 'model' parameter values
%
% GLOBAL VARIABLE: prior_
%
% INCLUDES: procedures for evaluating log pdfs

% Sungbae An
% 07/21/2005

global prior_

y = 0;
a = 0;
b = 0;

nx  = length(x);

for i=1:nx

	shape = prior_.shape(i);
	para1 = prior_.para1(i);
	para2 = prior_.para2(i);

	% BETA prior
	if shape==1
		a = (1-para1)*para1^2/para2^2 - para1;
		b = a*(1/para1 - 1);
		y = y + lnpdfbeta(x(i),a,b);

	% GAMMA prior
	elseif shape==2
		b = para2^2/para1;
		a = para1/b;
		y = y + lnpdfgam(x(i),a,b);

	% NORMAL prior
	elseif shape==3
		a = para1;
		b = para2;
		y = y + lnpdfnorm(x(i),a,b);

	% INVGAMMA prior
	elseif shape==4
		a = para1;
		b = para2;
		y = y + lnpdfig(x(i),a,b);

	% uniform
	elseif shape==5
		a = para1;
		b = para2;
		y = y - log(b-a);
	end;
end;


%
% logs of density functions
%


% beta
function y=lnpdfbeta(x,a,b)
% LNPDFBETA(X,A,B)
%	calculates log BETA(A,B) at X

% Sungbae An
% 03/03/2002
if x>0 & x<1
y = gammaln(a+b) - gammaln(a) - gammaln(b) + (a-1)*log(x) + (b-1)*log(1-x);
else
y = -1000000000; 
end 

% gamma
function y=lnpdfgam(x,a,b)
% LNPDFGAM(X,A,B)
%	calculates log GAMMA(A,B) at X

% 03/03/2002
% Sungbae An

if x>0 
y = -gammaln(a) -a*log(b)+ (a-1)*log(x) -x/b ;
else 
y = -10000000000;
end 
% inverse gamma
function y = lnpdfig(x,a,b)
% LNPDFIG(X,A,B)
%	calculates log INVGAMMA(A,B) at X

% 03/03/2002
% Sungbae An
if x<=0 
y= -1e20;  return; 
else
y = log(2) - gammaln(b/2) + (b/2)*log(b*a^2/2) - ( (b+1)/2 )*log(x^2) - b*a^2/(2*x^2);
end 
% normal
function y = lnpdfnorm(x,a,b);
% LNPDFNORM(X,A,B)
%	calculates log NORMAL(A,B) at X

% 03/03/2002
% Sungbae An
y = -0.5*log(2*pi) - log(b) - 0.5*(x-a)^2/b^2;

